class Solution {
    public int findMaxLength(int[] nums) {
        int n = nums.length;
        for(int i = 0;i < n;i++)
            if(nums[i] == 0) nums[i] = -1;

        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,-1);
        int sum = 0,count = 0;
        for(int i = 0;i < n;i++) {
            sum += nums[i];
            if(hash.get(sum) != null)
                count = Math.max(count,i - hash.get(sum));
            if(hash.get(sum) == null)
                hash.put(sum,i);
        }
        return count;
    }
}